package com.app.web.front.controller;

import java.util.List;

import com.app.util.AjaxResult;
import com.app.util.Const;
import com.app.util.DateUtils;
import com.app.util.FileUtils;
import com.app.web.BaseController;
import com.app.web.adimin.model.User;
import com.app.web.front.model.Category;
import com.app.web.front.model.Comment;
import com.app.web.front.model.Reply;
import com.app.web.front.model.Topic;
import com.jfinal.kit.PathKit;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.upload.UploadFile;

public class TopicController extends BaseController{
	
	
	public void list(){
		
		AjaxResult result = new AjaxResult(1);
		int pageNumber = this.getParaToInt(0,1);  //当前页
		int pageSize = this.getParaToInt("pageSize", 10);
		
		Record params = this.getParamMap("cate_id", "type", "search");
		User user=this.getCurrUser();//当前用户
		Page<Record> topics = Topic.dao.getTopicAll(pageNumber, pageSize, params ,user);
		result.setData("datas", topics);
		this.renderJson(result.toJson());
	}
	
	
	/**
	 * 根据类别ID 查询 帖子
	 */
	public void findByCateId(){
		AjaxResult result = new AjaxResult(1);
		int pageNumber = this.getParaToInt(0,1);
		int categoryId = this.getParaToInt("category_id");
		
		Page<Record> topics = Topic.dao.paginateByCateId(pageNumber, 2, categoryId);
		Category category = Category.dao.findById(categoryId);
		result.setData("datas", topics);
		result.setData("category_name", category.get("name"));
		
		this.renderJson(result.toJson());
	}
	
	public void addOrUpdate() throws Exception{
		AjaxResult result = new AjaxResult(1,"操作成功");
		try {
			Topic topic = this.getModel(Topic.class).setAttrs(this.getParamMap());
			if(StrKit.notNull(topic.get("id"))){
				topic.update();
			}else{
				topic.set("create_time", DateUtils.getCurrDateTime());
				topic.set("user_id", this.getCurrUser().get("id"));
				topic.save();
				
				Category.dao.updateTopicCount(topic.get("category_id")); // 更新对应类别的话题数量
				
				result.setData("data", topic);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			result.setMsg(0, "提交失败："+e.getMessage());
		}finally{
			this.renderJson(result.toJson());
		}
	}
	
	public void info(){
		AjaxResult result = new AjaxResult(1);
		int topicId = this.getParaToInt("topic_id");
		Record topic = Topic.dao.getTopic(topicId);
		List<Record> replies = Topic.dao.getReplies(topicId);
		
		// 更新查询次数
		Topic.dao.updateViewCount(topicId);
		
		result.setData("topic", topic);
		result.setData("replies", replies);
		this.renderJson(result.toJson());
	}
	
	// 回复话题
	public void addReply() throws Exception{
		AjaxResult result = new AjaxResult(1,"回复成功");
		try {
			Reply reply = this.getModel(Reply.class).setAttrs(this.getParamMap());
			reply.set("create_time", DateUtils.getCurrDateTime());
			reply.set("user_id", this.getCurrUser().get("id"));
			reply.set("pid", 0);
			reply.save();
			// 更新回复次数
			Topic.dao.updateReplyCount(reply.get("topic_id"));
			
			result.setData("reply", Reply.dao.getReply(reply.getInt("id")));
			
		} catch (Exception e) {
			result.setFailure("回复失败:"+e.getMessage());
			throw new Exception(e);
		}finally{
			this.renderJson(result.toJson());
		}
	}
	//评论回复
	public void addComment(){
		AjaxResult result = new AjaxResult(1,"评论成功");
		try {
			Comment comment = this.getModel(Comment.class).setAttrs(this.getParamMap());
			comment.set("sender_id", this.getCurrUser().get("id"));
			comment.set("create_time", DateUtils.getCurrDateTime());
			comment.save();
			result.setData("comment", Comment.dao.getComment(comment.getInt("id")));
		} catch (Exception e) {
			e.printStackTrace();
			result.setFailure("评论失败:"+e.getMessage());
		}
		
		this.renderJson(result.toJson());
		
	}
	
	public void findBySearch(){
		AjaxResult result = new AjaxResult(1,"评论成功");
		int pageNumber = this.getParaToInt(0,1);
		String search = this.getPara("search", "");
		
		Page<Record> topics = Topic.dao.paginateBySearch(pageNumber, 2, search);
		result.setData("datas", topics);
		result.setData("category_name", "搜索");
		
		this.renderJson(result.toJson());
	}
	
	/***
	 * uploadimage == {state:'SUCCESS', url:''}
	 * listimage   == {list:[], start:0, state:"SUCCESS", total:29}
	 * 上传图片
	 */
	public void upload(){
		AjaxResult result = new AjaxResult(1);
		String action = this.getPara("action", "");
		String path = PathKit.getWebRootPath() + Const.TOPIC_PATH;
		if(action.equals("uploadimage")){
			UploadFile uploadFile = this.getFile("img", path);
			String url =  Const.TOPIC_PATH+"/"+uploadFile.getFileName();
			
			result.setData("error", 0);
			result.setData("url", url);
			result.setData("state", "SUCCESS");
			
		}else if(action.equals("listimage")){
			FileUtils.getFileList(path);
		}
		this.renderJson(result.toJson());
	}
}
